﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SystemHYS.API.Application.command.GualityGroupCommand;
using SystemHYS.API.DTOS;
using SystemHYS.Domian;
using SystemHYS.Domian.DTO;

namespace SystemHYS.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class GualityGroupController : ControllerBase
    {
        IMediator mediatR;
        public GualityGroupController(IMediator mediatR)
        {
            this.mediatR = mediatR;
        }
        /// <summary>
        /// 获取质检组信息
        /// </summary>
        /// <param name="command">查询条件</param>
        /// <returns>返回质检组信息</returns>
        [HttpGet]
        public async Task<Apiresult<PageDTO<GualityGroup>>> GetGualityGroup([FromQuery]GualityGroupShowCommand command)
        {
            Apiresult<PageDTO<GualityGroup>> apiresult = new Apiresult<PageDTO<GualityGroup>>();
            try
            {
                apiresult.Data = await mediatR.Send(command);
            }
            catch (Exception ex)
            {
                apiresult.Code = -1;
                apiresult.Message = ex.Message;
                throw;
            }
            return apiresult;
        }
        /// <summary>
        /// 下拉质检组信息
        /// </summary>
        /// <param name="command">查询条件</param>
        /// <returns>返回质检组信息</returns>
        [HttpGet]
        public async Task<Apiresult<List<GualityGroup>>> GetGualityGroupSelect()
        {
            Apiresult<List<GualityGroup>> apiresult = new Apiresult<List<GualityGroup>>();
            try
            {
                apiresult.Data = await mediatR.Send(new GualityGroupSelectCommand());
            }
            catch (Exception ex)
            {
                apiresult.Code = -1;
                apiresult.Message = ex.Message;
                throw;
            }
            return apiresult;
        }
        /// <summary>
        /// 添加质检组信息
        /// </summary>
        /// <param name="command">质检组信息</param>
        /// <returns>返回受影响信息</returns>
        [HttpPost]
        public async Task<Apiresult<int>> AddGualityGroup(GualityGroupCreateCommand command)
        {
            Apiresult<int> apiresult = new Apiresult<int>();
            try
            {
                apiresult.Data = await mediatR.Send(command);
            }
            catch (Exception ex)
            {
                apiresult.Code = -1;
                apiresult.Message = ex.Message;
                throw;
            }
            return apiresult;
        }
        /// <summary>
        /// 删除质检组信息
        /// </summary>
        /// <param name="command">质检组id</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<Apiresult<int>> DeleteGualityGroup(GualityGroupDeleteCommand command)
        {
            Apiresult<int> apiresult = new Apiresult<int>();
            try
            {
                apiresult.Data = await mediatR.Send(command);
            }
            catch (Exception ex)
            {
                apiresult.Code = -1;
                apiresult.Message = ex.Message;
                throw;
            }
            return apiresult;
        }
        [HttpGet]
        public async Task<Apiresult<GualityGroup>> ModelGualityGroup([FromQuery]GualityGroupModelCommand command)
        {
            Apiresult<GualityGroup> apiresult = new Apiresult<GualityGroup>();
            try
            {
                apiresult.Data = await mediatR.Send(command);
            }
            catch (Exception ex)
            {
                apiresult.Code = -1;
                apiresult.Message = ex.Message;
                throw;
            }
            return apiresult;
        }
        [HttpPost]
        public async Task<Apiresult<int>> UpdateGualityGroup(GualityGroupUpdateCommand command)
        {
            Apiresult<int> apiresult = new Apiresult<int>();
            try
            {
                apiresult.Data = await mediatR.Send(command);
            }
            catch (Exception ex)
            {
                apiresult.Code = -1;
                apiresult.Message = ex.Message;
                throw;
            }
            return apiresult;
        }
    }
}
